home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / adainc / s-pooloc.adb < prev    next >
Text File  |  1996-01-30  |  6KB  |  157 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                    S Y S T E M . P O O L _ L O C A L                     --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.6 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- The GNAT library is free software; you can redistribute it and/or modify --
  14. -- it under terms of the GNU Library General Public License as published by --
  15. -- the Free Software  Foundation; either version 2, or (at your option) any --
  16. -- later version.  The GNAT library is distributed in the hope that it will --
  17. -- be useful, but WITHOUT ANY WARRANTY;  without even  the implied warranty --
  18. -- of MERCHANTABILITY  or  FITNESS FOR  A PARTICULAR PURPOSE.  See the  GNU --
  19. -- Library  General  Public  License for  more  details.  You  should  have --
  20. -- received  a copy of the GNU  Library  General Public License  along with --
  21. -- the GNAT library;  see the file  COPYING.LIB.  If not, write to the Free --
  22. -- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with System.Storage_Pools;    use System.Storage_Pools;
  27. with System.Storage_Elements; use System.Storage_Elements;
  28. with System.Address_To_Access_Conversions;
  29.  
  30. package body System.Pool_Local is
  31.  
  32.    Pointer_Size  : constant Storage_Offset := Address'Size / Storage_Unit;
  33.    Pointers_Size : constant Storage_Offset := 2 * Pointer_Size;
  34.  
  35.    type Acc_Address is access all Address;
  36.    package Addr is new Address_To_Access_Conversions (Address);
  37.  
  38.    --------------------------
  39.    -- Imported C Functions --
  40.    --------------------------
  41.  
  42.    --  It is assumed that these functions are self-protected against concurrent
  43.    --  access (should be true on a POSIX system with threads, and is part of
  44.    --  the interface requirement for the implementation of Tasking.Primitives)
  45.  
  46.    function malloc (Size : Storage_Count) return System.Address;
  47.    pragma Import (C, malloc, "malloc");
  48.  
  49.    procedure free (Address : System.Address);
  50.    pragma Import (C, free, "free");
  51.  
  52.    -----------------------
  53.    -- Local Subprograms --
  54.    -----------------------
  55.  
  56.    function Next (A : Address) return Acc_Address;
  57.    --  Given an address of a block, return an access to the next block
  58.  
  59.    function Prev (A : Address) return Acc_Address;
  60.    --  Given an address of a block, return an access to the previous block
  61.  
  62.    --------------
  63.    -- Allocate --
  64.    --------------
  65.  
  66.    procedure Allocate
  67.      (Pool         : in out Unbounded_Reclaim_Pool;
  68.       Address      : out System.Address;
  69.       Storage_Size : Storage_Count;
  70.       Alignment    : Storage_Count)
  71.    is
  72.       Allocated : constant System.Address
  73.         := malloc (Storage_Size + Pointers_Size);
  74.  
  75.    begin
  76.       --  The call to malloc returns an address whose alignment is compatible
  77.       --  with the worst case alignment requirement for the machine; thus the
  78.       --  Alignment argument can be safely ignored.
  79.  
  80.       if Allocated = Null_Address then
  81.          raise Storage_Error;
  82.       else
  83.          Address := Allocated + Pointers_Size;
  84.          Next (Allocated).all  := Pool.First;
  85.          Prev (Allocated).all  := Null_Address;
  86.  
  87.          if Pool.First /= Null_Address then
  88.             Prev (Pool.First).all := Allocated;
  89.          end if;
  90.  
  91.          Pool.First := Allocated;
  92.       end if;
  93.    end Allocate;
  94.  
  95.    ----------------
  96.    -- Deallocate --
  97.    ----------------
  98.  
  99.    procedure Deallocate
  100.      (Pool         : in out Unbounded_Reclaim_Pool;
  101.       Address      : System.Address;
  102.       Storage_Size : Storage_Count;
  103.       Alignment    : Storage_Count)
  104.    is
  105.       Allocated : constant System.Address := Address - Pointers_Size;
  106.  
  107.    begin
  108.       if Prev (Allocated).all = Null_Address then
  109.          Pool.First := Next (Allocated).all;
  110.          Prev (Pool.First).all := Null_Address;
  111.       else
  112.          Next (Prev (Allocated).all).all := Next (Allocated).all;
  113.       end if;
  114.  
  115.       if Next (Allocated).all /= Null_Address then
  116.          Prev (Next (Allocated).all).all := Prev (Allocated).all;
  117.       end if;
  118.  
  119.       free (Allocated);
  120.    end Deallocate;
  121.  
  122.    --------------
  123.    -- Finalize --
  124.    --------------
  125.  
  126.    procedure Finalize (Pool : in out Unbounded_Reclaim_Pool) is
  127.       N         : System.Address := Pool.First;
  128.       Allocated : System.Address;
  129.  
  130.    begin
  131.       while N /= Null_Address loop
  132.          Allocated := N;
  133.          N := Next (N).all;
  134.          free (Allocated);
  135.       end loop;
  136.    end Finalize;
  137.  
  138.    ----------
  139.    -- Next --
  140.    ----------
  141.  
  142.    function Next (A : Address) return Acc_Address is
  143.    begin
  144.       return Acc_Address (Addr.To_Pointer (A));
  145.    end Next;
  146.  
  147.    ----------
  148.    -- Prev --
  149.    ----------
  150.  
  151.    function Prev (A : Address) return Acc_Address is
  152.    begin
  153.       return Acc_Address (Addr.To_Pointer (A + Pointer_Size));
  154.    end Prev;
  155.  
  156. end System.Pool_Local;
  157.